package remindrepository

import (
	"time"
	ds "zzxq/common/datasource"
	"zzxq/common/logger"
	"zzxq/models"
	"zzxq/tools"
)

type DbRemind struct {}

//添加数据
func (r *DbRemind) Create(uid int, title string, btime time.Time) bool {
	schedule := models.Remind{
		Uid: uid,
		Title: title,
		Btime: btime.Format("2006-01-02 15:04:05"),
		Status: 0,
		Ctime: time.Now().Format("2006-01-02 15:04:05"),
	}

	if err := ds.GetDb().Create(&schedule).Error; err != nil {
		logger.GetLog().Error("添加数据异常", err)

		return false
	}

	return true
}

//删除数据
func (r *DbRemind) Delete(id int, uid int) bool {
	schedule := models.Remind{}

	if err := ds.GetDb().Where("id = ? AND uid = ?", id, uid).Delete(&schedule).Error; err != nil {
		logger.GetLog().Error("删除数据异常", err)

		return false
	}

	return true
}

//获取事项列表
func (r *DbRemind) GetDateList(uid int, time time.Time) *[]models.Remind {
	var schedule []models.Remind

	startTime, endTime := tools.TimeDeginEnd(time)

	if err := ds.GetDb().Where("uid = ? AND btime BETWEEN ? AND ?", uid, startTime, endTime).Find(&schedule); err != nil {
		logger.GetLog().Error("获取数据异常", err)
	}

	return &schedule
}